<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    
    
    <title>数组与类数组 | Liuqh&#39;blogs | 要有最朴素的生活  最遥远的梦想，即使明日天寒地冻 路远马亡。</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="keywords" content="数组,类数组">
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.css">
    <link rel="stylesheet" href="/css/style.css?v=1.4.3">
    
    <script type="text/javascript">
        // Data Center
        var DC = {
            reward:	true,
            lv: JSON.parse('{"enable":false,"app_id":null,"app_key":null,"icon":true}'),
            v: JSON.parse('{"enable":false,"appid":null,"appkey":null,"notify":true,"verify":true,"placeholder":"give me some sugers plz...","avatar":"wavatar"}'),
            g: JSON.parse('{"enable":false,"lazy":false,"owner":"liuqinghua0609","repo":"gitment","oauth":{"client_id":null,"client_secret":null},"perPage":10}'),
            d: JSON.parse('{"app_id":null}')
        };
    </script>
    <script type="text/javascript">
        window.lazyScripts=[];
    </script>
    
</head>


<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap">
      
      <img src="/img/111.jpg" class="brand-bg">
      
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Liuqh</h5>
          <a href="mailto:心随远方 无畏无惧" title="心随远方 无畏无惧" class="mail">
            
              <span>心</span>
            
              <span>随</span>
            
              <span>远</span>
            
              <span>方</span>
            
              <span> </span>
            
              <span>无</span>
            
              <span>畏</span>
            
              <span>无</span>
            
              <span>惧</span>
            
          </a>
        </hgroup>
        
        <ul class="menu-link">
          
              <li>
                <a href="https://blog.csdn.net/qq_41840644" target="_blank">
                  <i class="icon icon-lg icon-heart"></i>
                </a>
              </li>
            
              <li>
                <a href="https://weibo.com/5902408774/profile?rightmod=1&amp;wvr=6&amp;mod=personnumber&amp;is_all=1" target="_blank">
                  <i class="icon icon-lg icon-weibo"></i>
                </a>
              </li>
            
        </ul>
        
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                首页
              </a>
            </li>
        
            <li class="">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                分类
              </a>
            </li>
        
            <li class="">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                标签
              </a>
            </li>
        
            <li class="">
              <a href="/custom"  >
                <i class="icon icon-lg icon-music"></i>
                随身听
              </a>
            </li>
        
            <li class="">
              <a href="/about"  >
                <i class="icon icon-lg icon-user"></i>
                我
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row clearfix">
        <a href="javascript:;" class="header-icon pull-left waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">
            <span>数组与类数组</span>
            
        </div>
        
        <a href="javascript:;" id="site_search_btn" class="header-icon pull-right waves-effect waves-circle waves-light">
            <i class="icon icon-lg icon-search"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">
    <img src="/img/banner1.jpg" class="header-bg">
    <div class="container fade-scale">
        <h1 class="title">数组与类数组</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-07-17T15:31:18.000Z" itemprop="datePublished" class="page-time">
  2019-07-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript学习笔记/">JavaScript学习笔记</a></li></ul>

            
        </h5>
        
    </div>
    

</header>

<div id="site_search">
    <div class="search-title clearfix">
        <span class="pull-left">
          <i class="icon icon-lg icon-search"></i>
        </span>
        <input type="text" id="local-search-input" name="q" results="0" placeholder="search my blog..." class="form-control pull-left"/>
        <a href="javascript:;" class="close pull-right waves-effect waves-circle waves-light">
          <i class="icon icon-lg icon-close"></i>
        </a>
    </div>
    <div id="local-search-result"></div>
</div>


<div class="container body-wrap">
    <article id="post-数组与类数组"
  class="post-article article-type-post" itemprop="blogPost">
    <div class="post-card">
        <h1 class="post-card-title">数组与类数组</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-07-17 23:31:18" datetime="2019-07-17T15:31:18.000Z"  itemprop="datePublished">2019-07-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/JavaScript学习笔记/">JavaScript学习笔记</a></li></ul>



            

            


            
        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            
            <h3 id="一、数组"><a href="#一、数组" class="headerlink" title="一、数组"></a>一、数组</h3><h4 id="数组的定义："><a href="#数组的定义：" class="headerlink" title="数组的定义："></a>数组的定义：</h4><blockquote>
<ul>
<li>字面量<br><code>var arr = [1,2,3] //常用写法</code></li>
<li>new Array（ ）<br><code>new Array(10)  //一个参数的情况下表示生成了一个长度为10的数组</code><br><code>new Array(2,3,) // [2,3]</code></li>
</ul>
</blockquote>
<h4 id="数组的方法"><a href="#数组的方法" class="headerlink" title="数组的方法"></a>数组的方法</h4><blockquote>
<ol>
<li>改变原数组的方法</li>
</ol>
<ul>
<li>pop  </li>
<li>push   </li>
<li>shift   </li>
<li>unshift </li>
<li>splice</li>
<li>reverse</li>
</ul>
<ul>
<li>sort</li>
</ul>
<ol start="2">
<li>不改变原数组的方法</li>
</ol>
<ul>
<li>concat </li>
<li>slice</li>
<li>join</li>
<li>split</li>
<li>toString</li>
</ul>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr.pop()	<span class="comment">//3 删除数组的最后一位，并返回该数值 此时 arr = [1,2]</span></span><br><span class="line">arr.push(<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>) <span class="comment">// 5 向数组的最后添加元素，并返回添加之后的数组长度 arr= [1,2,4,5,6]</span></span><br><span class="line">arr.shift() 	<span class="comment">// 1 删除数组的第一位，并返回该值 arr = [2,4,5,6]</span></span><br><span class="line">arr.unshift(<span class="number">8</span>,<span class="number">9</span>) <span class="comment">// 6 向数组的头部添加元素，并返回数组的长度 arr = [8,9,2,4,5,6]</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>splice方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt; var arr = [1,2,3,4,5,6]</span><br><span class="line">&gt; arr.splice(开始截取的索引，截取的个数，从截取开始位加入的元素)</span><br><span class="line">&gt; arr.splice(0,1,2,3,4) //从下标为0的截取1个元素，并在开始截取的位置添加2,3,4 此时arr=[2,3,4,2,3,4,5,6]</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<p>reverse方法是翻转数组，就是讲数组逆序输出</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr.reverse()<span class="comment">//[3,2,1]</span></span><br></pre></td></tr></table></figure>

<p>sort 排序</p>
<blockquote>
<p>如果调用该方法时没有使用参数，将按字母顺序对数组中的元素进行排序，说得更精确点，是按照字符编码的顺序进行排序。所以有时他并不会按照我们认知的数字大小去排列，这时我们就需要往里面传入一个参数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">var</span> arr =[<span class="number">2</span>,<span class="number">5</span>,<span class="number">20</span>,<span class="number">78</span>,<span class="number">3</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">10</span>]</span><br><span class="line">&gt; <span class="comment">//升序</span></span><br><span class="line">&gt; arr.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">&gt;    <span class="keyword">if</span>(a&gt;b)&#123;</span><br><span class="line">&gt;       <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&gt;    &#125; <span class="keyword">else</span>&#123;</span><br><span class="line">&gt;       <span class="keyword">return</span> <span class="number">-1</span>;  </span><br><span class="line">&gt;    &#125;</span><br><span class="line">&gt; &#125;)</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>sort排序其实利用的就是冒泡排序的原理会循环比较两个值的大小然后进行位置的交换，规律：<br>当返回一个正值时，后面的数换到前面，相当于两数交换位置<br>当返回一个负值时，前面的数还在前面，两数位置不变</p>
</blockquote>
<h3 id="二、类数组"><a href="#二、类数组" class="headerlink" title="二、类数组"></a>二、类数组</h3><blockquote>
<p>类数组的定义：一种类似数组的对象，但是类数组不能直接调用数组的方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&gt; <span class="keyword">var</span> obj = &#123;</span><br><span class="line">      <span class="string">"2"</span>:<span class="string">"a"</span>,</span><br><span class="line">      <span class="string">"3"</span>:<span class="string">"b"</span>,</span><br><span class="line">      <span class="string">"length"</span>:<span class="number">2</span>,</span><br><span class="line">      <span class="string">"push"</span>:<span class="built_in">Array</span>.prototype.push,</span><br><span class="line">      <span class="string">"splice"</span>:<span class="built_in">Array</span>.prototype.splice</span><br><span class="line">    &#125;</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>像这样在类数组中添加对应属性，并将数组的方法赋值给它，就可以调用了</p>
</blockquote>
<h3 id="三、代码模拟push方法"><a href="#三、代码模拟push方法" class="headerlink" title="三、代码模拟push方法"></a>三、代码模拟push方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.myPush = <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">for</span> (<span class="keyword">var</span> i =<span class="number">0</span>; i&lt; <span class="built_in">arguments</span>.length;i++)&#123;</span><br><span class="line">		<span class="keyword">this</span>[<span class="keyword">this</span>.length] = <span class="built_in">arguments</span>[i];</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.length;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        </div>
        
<blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2019-07-26T12:00:58.315Z" itemprop="dateUpdated">2019-07-26 20:00:58</time>
</span><br>


        
        转载注明出处，原文地址：<a href="/2019/07/17/数组与类数组/" target="_blank" rel="external">http://liuqinghua0609.gitee.io/2019/07/17/数组与类数组/</a>
        
    </div>
    <footer>
        <a href="http://liuqinghua0609.gitee.io">
            <img src="/img/avatar.jpg" alt="Liuqh">
            Liuqh
        </a>
    </footer>
</blockquote>

        
            <div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>

            
        
        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/数组/">数组</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/类数组/">类数组</a></li></ul>

            <div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://liuqinghua0609.gitee.io/2019/07/17/数组与类数组/&title=《数组与类数组》 — Liuqh'blogs&pic=http://liuqinghua0609.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://liuqinghua0609.gitee.io/2019/07/17/数组与类数组/&title=《数组与类数组》 — Liuqh'blogs&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>

        </div>
        
            


        
    </div>
    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="prev">
      <a href="/2019/07/17/对象和包装类/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">对象和包装类</h4>
      </a>
    </div>
  

  
</nav>


    
    
        <aside class="post-widget">
            <nav class="post-toc-wrap" id="post-toc">
                <strong>目录</strong>
                <ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#一、数组"><span class="post-toc-number">1.</span> <span class="post-toc-text">一、数组</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#数组的定义："><span class="post-toc-number">1.1.</span> <span class="post-toc-text">数组的定义：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#数组的方法"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">数组的方法</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#二、类数组"><span class="post-toc-number">2.</span> <span class="post-toc-text">二、类数组</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#三、代码模拟push方法"><span class="post-toc-number">3.</span> <span class="post-toc-text">三、代码模拟push方法</span></a></li></ol>
            </nav>
            <div class="toc-bar"><div>
        </aside>
    
</article>

    <div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        我们一起来让这个世界有趣一点
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/reward-wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/reward-wechat.jpg" data-alipay="/img/reward-alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>

    

</div>

    </main>
    <footer class="footer ">
    
    <div class="bottom">
        <p>
            <span>
                Liuqh &copy; 2017 - 2019
            </span>
        		
           	
            
            
            <span>
	            Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/codefine/hexo-theme-mellow" target="_blank">mellow</a>
            </span>
            
            
            

            
                
<span class="site-uv" title="总访客量">
    <i class="icon icon-user"></i>
    <i class="busuanzi-value" id="busuanzi_value_site_uv"></i>
</span>


<span class="site-pv" title="总访问量">
    <i class="icon icon-eye"></i>
    <i class="busuanzi-value" id="busuanzi_value_site_pv"></i>
</span>

            
        </p>
    </div>
</footer>

    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://liuqinghua0609.gitee.io/2019/07/17/数组与类数组/&title=《数组与类数组》 — Liuqh'blogs&pic=http://liuqinghua0609.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://liuqinghua0609.gitee.io/2019/07/17/数组与类数组/&title=《数组与类数组》 — Liuqh'blogs&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>


    
    <!-- main-js -->
<script type="text/javascript" src="//cdn.bootcss.com/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="/js/plugins/fastclick.js?v=1.4.3"></script>
<script type="text/javascript" src="/js/plugins/ios-orientationchange-fix.js?v=1.4.3"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.js"></script>

<script type="text/javascript" src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>

<script type="text/javascript" src="/js/method.js?v=1.4.3"></script>
<script type="text/javascript" src="/js/blog.js?v=1.4.3"></script>

<!-- third-party -->






<script type="text/javascript" src="/js/plugins/local_search.js?v=1.4.3"></script>
<script type="text/javascript">
	var search_path = "search.xml";
	if (search_path.length === 0) {
		search_path = "search.xml";
	}
	var path = "/" + search_path;
	searchFunc(path, "local-search-input", "local-search-result");
</script>



<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



    
    





    <!-- mathjax config similar to math.stackexchange -->
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
            processEscapes: true,
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>







    
</body>
</html>
